package org.lzy.shop.adapter

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import org.lzy.shop.order.FragmentOrder


class MyOrderFragAdapter(fm: FragmentActivity) : FragmentStateAdapter(fm) {
    // 状态常量定义
    companion object {
        private const val STATUS_UNPAID = 0 // 待付款
        private const val STATUS_UNSHIPPED = 1 // 待发货
        private const val STATUS_SHIPPED = 2 // 待收货
        private const val STATUS_TO_BE_EVALUATED = 3 // 待评价
        private const val STATUS_COMPLETED = 4 // 交易完成
        private const val STATUS_REFUNDING = 5 // 退款中
        private const val STATUS_REFUNDED = 6 // 已退款
        private const val STATUS_CLOSED = 7 // 已关闭
    }


    override fun createFragment(position: Int): Fragment {
        val fragment = FragmentOrder.newInstance(getStatusArrayByPosition(position))
        // 也可以通过arguments传递状态信息
        val bundle = Bundle()
        bundle.putIntArray("status", getStatusArrayByPosition(position))
        fragment.arguments = bundle
        return fragment

    }

    override fun getItemCount(): Int {
        return 8
    }

    /**
     * 根据位置获取对应的订单状态数组
     */
    private fun getStatusArrayByPosition(position: Int): IntArray {
        return when (position) {
            0 -> intArrayOf(STATUS_UNPAID) // 待付款
            1 -> intArrayOf(STATUS_UNSHIPPED) // 待发货
            2 -> intArrayOf(STATUS_SHIPPED) // 待收货
            3 -> intArrayOf(STATUS_TO_BE_EVALUATED) // 待评价
            4 -> intArrayOf(STATUS_COMPLETED) // 交易完成
            5 -> intArrayOf(STATUS_REFUNDING) // 退款中
            6 -> intArrayOf(STATUS_REFUNDED) // 已退款
            7 -> intArrayOf(STATUS_CLOSED) // 已关闭
            else -> intArrayOf() // 默认返回全部
        }
    }
}
